using AutoMapper;
using Common.Commands.AccountCommands;
using Common.Commands.AttachmentCommands;
using Common.Commands.ContactCommands;
using Common.Commands.FolderCommands;
using Common.Commands.LeadCommands;
using Common.Commands.MemberCommands;
using Common.Commands.NewsLetterCommands;
using Common.Commands.NoteCommands;
using Common.Commands.TemplateCommands;
using Common.Commands.TrackLogCommands;
using Common.Enumerations;
using Dto.ApiRequests.AccountForms;
using Dto.ApiRequests.AttachmentForms;
using Dto.ApiRequests.ConfigForms;
using Dto.ApiRequests.ContactForms;
using Dto.ApiRequests.FolderForms;
using Dto.ApiRequests.LeadForms;
using Dto.ApiRequests.MemberForms;
using Dto.ApiRequests.NewsLetter;
using Dto.ApiRequests.NoteForms;
using Dto.ApiRequests.TemplateForms;
using Dto.ApiRequests.TrackLogForms;
using Sanelib.Common.Helpers;
using Sanelib.Common.SystemSettings;
using Sanelib.Dto.Enumerations;
using WebApp.Initialization.Automapper.Resolvers;

namespace WebApp.Initialization.Automapper
{
    public class FormToCommandProfile : Profile
    {
        protected override void Configure()
        {
            //Member
            CreateMap<UpdateMemberForm, UpdateMember>()
                .ForMember(x => x.Role, o => o.MapFrom(x => Role.FromValue(x.RoleValue)))
                .ForMember(x => x.UserStatus, o => o.MapFrom(x => UserStatus.FromValue(x.UserStatusValue)))
                .ForMember(x => x.TagId, o => o.Ignore())
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<AddMemberForm, AddMember>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.Role, o => o.MapFrom(x => Role.FromValue(x.RoleValue)))
                .ForMember(x => x.UserStatus, o => o.MapFrom(x => UserStatus.FromValue(x.UserStatusValue)))
                .ForMember(x => x.TagId, o => o.Ignore())
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<TrackLogForm, AddTrackLog>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.UserId, o => o.Ignore())
                .ForMember(x => x.Name, o => o.Ignore())
                .ForMember(x => x.Description, o => o.Ignore())
                .ForMember(x => x.ImageData, o => o.Ignore());

            CreateMap<MemberCardRegisterForm, RegisterMemberCard>()
                .ForMember(d => d.Id, o => o.ResolveUsing<MemberCodeToIdResolver>().FromMember(s => s.Code));

            //Configuration
            CreateMap<SmsConfigForm, SmsConfig>();
            CreateMap<NetworkConfigForm, NetworkConfig>();
            CreateMap<EmailConfigForm, EmailConfig>();

            //Templete
            CreateMap<AddTemplateForm, AddTemplete>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()));

            CreateMap<UpdateTemplateForm, UpdateTemplete>();

            //Contact
            CreateMap<AddContactForm, AddContact>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.NameTitle, o => o.MapFrom(x => NameTitle.FromValue(x.NameTitle)))
                .ForMember(x => x.Designation, o => o.MapFrom(x => Designation.FromValue(x.Designation)))
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<UpdateContactForm, UpdateContact>()
                .ForMember(x => x.NameTitle, o => o.MapFrom(x => NameTitle.FromValue(x.NameTitle)))
                .ForMember(x => x.Designation, o => o.MapFrom(x => Designation.FromValue(x.Designation)))
                .ForMember(x => x.UserId, o => o.Ignore());

            //AssignedContact
            CreateMap<AddAssignedContactForm, AddAssignedContact>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.EntityType, o => o.MapFrom(x => EntityType.FromValue(x.EntityTypeValue)))
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<UpdateAssignedContactForm, UpdateAssignedContact>()
                .ForMember(x => x.EntityType, o => o.MapFrom(x => EntityType.FromValue(x.EntityTypeValue)))
                .ForMember(x => x.UserId, o => o.Ignore());

            //Account
            CreateMap<AddAccountForm, AddAccount>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.AccountType, o => o.MapFrom(x => AccountType.FromValue(x.AccountTypeValue)))
                .ForMember(x => x.Industry, o => o.MapFrom(x => Industry.FromValue(x.IndustryValue)))
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<UpdateAccountForm, UpdateAccount>()
                .ForMember(x => x.AccountType, o => o.MapFrom(x => AccountType.FromValue(x.AccountTypeValue)))
                .ForMember(x => x.Industry, o => o.MapFrom(x => Industry.FromValue(x.IndustryValue)))
                .ForMember(x => x.UserId, o => o.Ignore());

            //Account Case
            CreateMap<AddFolderForm, AddFolder>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.EntityType, o => o.MapFrom(x => EntityType.FromValue(x.EntityTypeValue)))
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<UpdateFolderForm, UpdateFolder>()
                .ForMember(x => x.EntityType, o => o.MapFrom(x => EntityType.FromValue(x.EntityTypeValue)))
                .ForMember(x => x.UserId, o => o.Ignore());

            //Note
            CreateMap<AddNoteForm, AddNote>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.EntityType, o => o.MapFrom(x => EntityType.FromValue(x.EntityTypeValue)))
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<UpdateNoteForm, UpdateNote>()
                .ForMember(x => x.EntityType, o => o.MapFrom(x => EntityType.FromValue(x.EntityTypeValue)))
                .ForMember(x => x.UserId, o => o.Ignore());

            // Attachment
            CreateMap<AttachmentUploadForm, AddAttachment>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.FileData, o => o.Ignore())
                .ForMember(x => x.FileHashCode, o => o.Ignore())
                .ForMember(x => x.EntityType, o => o.MapFrom(x => EntityType.FromValue(x.EntityTypeValue)))
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<UpdateAttachmentForm, UpdateAttachment>()
                .ForMember(x => x.Name, o => o.Ignore())
                .ForMember(x => x.ImageData, o => o.Ignore())
                .ForMember(x => x.UserId, o => o.Ignore());

            //Lead
            CreateMap<AddLeadForm, AddLead>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<AddAnonymousLeadForm, AddAnonymousLead>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()));

            CreateMap<UpdateLeadForm, UpdateLead>()
                .ForMember(x => x.UserId, o => o.Ignore());

            // Track            
            CreateMap<AddTrackLogForm, AddTrackLog>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()))
                .ForMember(x => x.UserId, o => o.Ignore());

            CreateMap<UpdateTrackLogForm, UpdateTrackLog>()
                .ForMember(x => x.UserId, o => o.Ignore());

            //NewsLetter
            CreateMap<AddNewsLetterForm, AddNewsLetter>()
                .ForMember(x => x.Id, o => o.MapFrom(x => GuidComb.New()));

            CreateMap<UpdateNewsLetterForm, UpdateNewsLetter>();
        }
    }
}